﻿using BAL;
using DAL;
using DAL.DataProvider;
using DAL.DataTransferObject;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Module.QLHoaDon
{
    public partial class frmThemChiTietHoaDon : Form
    {
        int _mode;
        HoaDon hdOBJ;
        ChiTietHoaDon ctObj;
        public frmThemChiTietHoaDon()
        {
            InitializeComponent();
            _mode = 1;
        }

        public frmThemChiTietHoaDon(string maHDMoi)
        {
            InitializeComponent();
            hdOBJ = new HoaDon();
            hdOBJ.maHoaDon = maHDMoi;
            entryQuantity = "0";
            _mode = 1;
        }

        string entryQuantity;
        public frmThemChiTietHoaDon(ChiTietHoaDon ctHD)
        {
            InitializeComponent();
            ctObj = ctHD;
            txtMaHang.Text = ctHD.maHang;
            entryQuantity = txtSoluong.Text = ctHD.soLuongMua.ToString();
            txtMaHang.ReadOnly = txtTenHang.ReadOnly = true;
            _mode = 2;
        }

        ChiTietHoaDonBAO ctBAO = new ChiTietHoaDonBAO();
        KhachHangBAO khBAO = new KhachHangBAO();
        NhanVienBAO nvBAO = new NhanVienBAO();
        HoaDonDAO hdDAO = new HoaDonDAO();
        HangHoaBAO hhBAO = new HangHoaBAO();

        private void btnOK_Click(object sender, EventArgs e)
        {
            string maHang = txtMaHang.Text.ToUpper();
            if (!maHang.Contains("a") && !maHang.Contains("b") && !maHang.Contains("c") && maHang != "H000")
                try
                {
                    if (hhBAO.SelectNameHangHoa(txtMaHang.Text).Rows.Count == 0)
                        throw new Exception("Khong co ma hang nao !");

                    DataTable dt;
                    if (_mode == 1)
                        dt = ctBAO.SelectAllChiTietHoaDon(hdOBJ.maHoaDon);
                    else
                        dt = ctBAO.SelectAllChiTietHoaDon(ctObj.maHoaDon);
                    DataView dv = new DataView(dt);
                    dv.RowFilter = String.Format("maHang like '{0}'", txtMaHang.Text);

                    if (dv.Count == 1 && _mode == 1)
                        throw new Exception("Da ton tai ma hang trong hoa don");
                    txtTenHang.Text = hhBAO.SelectNameHangHoa(txtMaHang.Text).Rows[0]["TenHang"].ToString();

                    try
                    {
                        if (Convert.ToInt32(txtSoluong.Text) <= 0)
                            throw new Exception("So luong phai lon hon hoac bang 0");
                        else
                        {
                            HangHoa hhObj = hhBAO.GetHangHoaObj(txtMaHang.Text);
                            if (hhObj.soLuong == 0)
                                throw new Exception("Hàng đã hết hoặc không còn được bán nữa !");
                            if (Convert.ToInt32(txtSoluong.Text) > hhObj.soLuong)
                            {
                                hhObj.soLuong = 0;
                                txtSoluong.Text = hhObj.soLuong.ToString();
                            }
                            else
                            {
                                hhObj.soLuong = hhObj.soLuong + Convert.ToInt32(entryQuantity) - Convert.ToInt32(txtSoluong.Text);
                            }


                            hhObj.maHang = txtMaHang.Text;
                            hhBAO.HangHoa_Update(hhObj);
                            ChiTietHoaDon ctHD = new ChiTietHoaDon();
                            //DataTable dtHangHoa=hhBAO.SelectNameHangHoa(hhObj.maHang);
                            ctHD.giaTruocVAT = hhObj.giaGoc;
                            if (_mode == 1)
                                ctHD.maHoaDon = hdOBJ.maHoaDon.ToUpper();
                            else
                                ctHD.maHoaDon = ctObj.maHoaDon.ToUpper();
                            ctHD.maGiamGiaHang = "0";
                            ctHD.maHang = hhObj.maHang;
                            ctHD.VAT = hhObj.VAT;
                            ctHD.soLuongMua = Convert.ToInt32(txtSoluong.Text);
                            //MessageBox.Show(_mode.ToString());
                            if (_mode == 1)
                                ctBAO.InsertChiTietHoaDon(ctHD);
                            else
                                ctBAO.UpdateChiTietHoaDon(ctHD);
                            //Console.WriteLine("update");
                            this.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        txtSoluong.Focus();
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    txtMaHang.Focus();
                }
        }


        private void txtMaHang_TextChanged(object sender, EventArgs e)
        {
            string maHang = txtMaHang.Text;

            //if (!maHang.Contains("a") && !maHang.Contains("b") && !maHang.Contains("c") && maHang != "H000")
            if (txtMaHang.Text.Length == 4)
                if (hhBAO.SelectNameHangHoa(txtMaHang.Text).Rows.Count != 0)
                    txtTenHang.Text = hhBAO.SelectNameHangHoa(txtMaHang.Text.ToUpper()).Rows[0]["TenHang"].ToString();
                else
                    txtTenHang.Text = "";
            else
                txtTenHang.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmThemHoaDon_Load(object sender, EventArgs e)
        {
            AutoCompleteStringCollection source = new AutoCompleteStringCollection();

            foreach (HangHoa item in hhBAO.HangHoa_GetData())
                source.Add(item.tenHang);
            txtTenHang.AutoCompleteCustomSource = source;
        }

        private void txtTenHang_TextChanged(object sender, EventArgs e)
        {
            //SqlCommand cmd = new SqlCommand("select MaHang from Hanghoa where TenHang=@TenHang" +
            //    " and TenHang != 'Tat ca' and TenHang !=  'thuc pham' and TenHang !='gia dung' and TenHang != 'dien tu' ", DataAccess.GetConnection());
            SqlCommand cmd = new SqlCommand("select MaHang from Hanghoa where TenHang=@TenHang", DataAccess.GetConnection());
            cmd.Parameters.AddWithValue("@TenHang", txtTenHang.Text);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count == 0)
                txtMaHang.Text = "";
            else
                txtMaHang.Text = dt.Rows[0]["MaHang"].ToString();
        }



        //public List<> 
    }
}
